package com.mujin.keji.collectionsystem.widget;

import android.Manifest;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.pm.PackageManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.dialog.BaseNiceDialog;
import com.mujin.keji.collectionsystem.dialog.NiceDialog;
import com.mujin.keji.collectionsystem.dialog.ViewConvertListener;
import com.mujin.keji.collectionsystem.dialog.ViewHolder;
import com.mujin.keji.collectionsystem.module.main.dto.SavePhoneDto;
import com.mujin.keji.collectionsystem.module.main.viewmodel.MainViewModel;
import com.mujin.keji.collectionsystem.utils.DeviceUtils;
import com.mujin.keji.collectionsystem.utils.ToastUtils;

/**
 * 输入手机号对话框
 *
 * @author weishixiong
 * @Time 2018-05-24
 */
public class InputPhoneDailog {
    private MainViewModel mainViewModel;

    public interface CommitPhoneListener {
        void commitPhone(String phone);
    }

    private BaseNiceDialog mDialog;

    /**
     * 输入手机号对话框
     *
     * @param context
     */
    public BaseNiceDialog InputPhoneDailog(final BaseActivity context, final CommitPhoneListener listener) {
        mainViewModel = ViewModelProviders.of(context).get(MainViewModel.class);
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_input_phone)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        dialog.setOutCancel(false);
                        mDialog = dialog;
                        final EditText etPhone = (EditText) holder.getView(R.id.et_phone);
                        TextView tvOk = (TextView) holder.getView(R.id.tv_ok);
                        tvOk.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (TextUtils.isEmpty(etPhone.getText().toString()) || etPhone.getText().toString().length() < 11) {
                                    ToastUtils.showToast("请输入正确的手机号码");
                                } else if (!(etPhone.getText().toString().startsWith("1"))) {
                                    ToastUtils.showToast("请输入正确的手机号码");
                                } else {
                                    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
                                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                                        ToastUtils.showToast("未获取到电话权限，无法保存");
                                        return;
                                    }
                                    String imei = telephonyManager.getDeviceId();
                                    context.getLoadingDialog().showDialog();
                                    mainViewModel.getDtoLiveData(DeviceUtils.getUniqueId(context),imei, etPhone.getText().toString(), "").observe(context, new Observer<BaseDto<SavePhoneDto>>() {
                                        @Override
                                        public void onChanged(@Nullable BaseDto<SavePhoneDto> savePhoneDtoBaseDto) {
                                            context.getLoadingDialog().dismiss();
                                            dialog.dismiss();
                                            ToastUtils.showToast(savePhoneDtoBaseDto.getMsg());

                                        }
                                    });
                                }
                            }
                        });
                    }
                })

                .show(context.getSupportFragmentManager());
        return mDialog;
    }


}
